jsx3.Class.defineClass(
	"tibco.admin.monitormanage.general.MonitorManageUtils",                   // the full name of the class to create
    jsx3.lang.Object, //  extends
	null,  // does not implement an interface
	function(util) {

		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is a service status, this method will construct
		*  the appropriate displayable string
		*/
		util.handleServiceStatus = function(element,cdfkey, matrix, column, rownumber,server) {
			var status = element.innerHTML;
			status = status.replace("&nbsp;","");
			status = status.trim();
			status = util.handleServiceStatusExternalization(status);
			element.innerHTML = status;
		}
		util.handleServiceStatusExternalization = function(status) {
		        var server = tibco.admin.Main.getServer();
			if(status == "Running")
			{ status = server.getDynamicProperty("@MM@general@svcStatusRunning"); }
			else if(status == "Stopped")
			{ status = server.getDynamicProperty("@MM@general@svcStatusStopped"); }
			return status;
		}

		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is a service assembly sync status, this method will construct
		*  the appropriate displayable string
		*/
		util.handleSASyncStatus = function(element,cdfkey, matrix, column, rownumber,server) {
			var status = element.innerHTML;
			status = status.replace("&nbsp;","");
			status = status.trim();
			status = util.handleSASyncStatusExternalization(status);
			element.innerHTML = status;
		}

		util.handleSASyncStatusExternalization = function(status) {
		        var server = tibco.admin.Main.getServer();
			if(status == "IN_SYNC")
			{ status = server.getDynamicProperty("@MM@general@svcAsblSyncStatusInSync"); }
			else if(status == "OUT_OF_SYNC_REDEPLOY_REQD")
			{ status = server.getDynamicProperty("@MM@general@svcAsblSyncStatusOutOfSync"); }
			else if(status == "REDEPLOYABLE")
			{ status = server.getDynamicProperty("@MM@general@svcAsblSyncStatusRedeployable"); }
			return status;
		}

		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is a service assembly status, this method will construct
		*  the appropriate displayable string
		*/
		util.handleSAStatus = function(element,cdfkey, matrix, column, rownumber,server) {
			var status = element.innerHTML;
			status = status.replace("&nbsp;","");
			status = status.trim();
			status = util.handleSAStatusExternalization(status);
			element.innerHTML = status;
		}

		util.handleSAStatusExternalization = function(status) {
		        var server = tibco.admin.Main.getServer();
			if(status == "UNKNOWN")
			{ status = server.getDynamicProperty("@MM@general@svcAsblStatusUnknown"); }
			else if(status == "NOTDEPLOYED")
			{ status = server.getDynamicProperty("@MM@general@svcAsblStatusNotDeployed"); }
			else if(status == "DEPLOYED")
			{ status = server.getDynamicProperty("@MM@general@svcAsblStatusDeployed"); }
			else if(status == "DEPLOYED_WITH_ERROR")
			{ status = server.getDynamicProperty("@MM@general@svcAsblStatusDeployedWithError"); }
			else if(status == "RUNNING")
			{ status = server.getDynamicProperty("@MM@general@svcAsblStatusRunning"); }
			else if(status == "RUNNING_WITH_ERROR")
			{ status = server.getDynamicProperty("@MM@general@svcAsblStatusRunningWithError"); }
			return status;
		}

	        /**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is a resource type, this method will construct
		*  the appropriate displayable string
		*/
		util.handleResourceType = function(element,cdfkey, matrix, column, rownumber,server) {
			var resourceType = element.innerHTML;
			resourceType = resourceType.replace("&nbsp;","");
			resourceType = resourceType.trim();
			var server = tibco.admin.Main.getServer();
			if(resourceType == "COMPOSITE_SERVICE")
			{ resourceType = server.getDynamicProperty("@MM@general@resTypeCompositeSvc"); }
			else if(resourceType == "COMPONENT")
			{ resourceType = server.getDynamicProperty("@MM@general@resTypeComponent"); }
			else if(resourceType == "COMPOSITE_REFERENCE")
			{ resourceType = server.getDynamicProperty("@MM@general@resTypeCompositeRef"); }
			element.innerHTML = resourceType;
		}

		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is a node status, this method will construct
		*  the appropriate displayable string
		*/
		util.handleNodeStatus = function(element,cdfkey, matrix, column, rownumber,server) {
			var status = element.innerHTML;
			status = status.replace("&nbsp;","");
			status = status.trim();
			status = util.handleNodeStatusExternalization(status);
			element.innerHTML = status;
		}

		util.handleNodeStatusExternalization = function(status) {
		        var server = tibco.admin.Main.getServer();
			if(status == "Defined")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusDefined"); }
			else if(status == "Install Failed")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusInstallFail"); }
			else if(status == "Installed")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusInstalled"); }
			else if(status == "Uninstall Failed")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusUninstallFail"); }
			else if(status == "Installing")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusInstalling"); }
			else if(status == "Uninstalling")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusUninstalling"); }
			else if(status == "Delete Failed")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusdeleteFail"); }
			else if(status == "Installed/Stopped")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusInstallStop"); }
			else if(status == "Start Failed")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusStartFail"); }
			else if(status == "Running")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusRunning"); }
			else if(status == "Stop Failed")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusStopFail"); }
			else if(status == "Starting")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusStarting"); }
			else if(status == "Stopping")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusStopping"); }
			else if(status == "Unknown")
			{ status = server.getDynamicProperty("@MM@general@nodeStatusUnknown"); }
			return status;
		}

		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is an image, this method will construct the appropriate URI for the
		*  image source.
		*/
		util.handleImgSrcURIResolving = function(element,cdfkey, matrix, column, rownumber,server) {
			var strHTML = element.innerHTML;

			if(strHTML != '')
			{
				var patternToMatch = /src=\"(.)*?\"/;
				var matchResults = patternToMatch.exec(strHTML)[0];
				var imgSrc = matchResults.substring(5,matchResults.length-1);

				var ogImgSrc = imgSrc;
				var pathIdx = imgSrc.indexOf("tibco/admin/monitormanage");
				if(pathIdx != -1 && pathIdx != 0)
				{ imgSrc = imgSrc.substring(pathIdx); }

				var newImgSrc = tibco.admin.Main.resolveURI("tibco.admin",imgSrc);
				element.innerHTML = strHTML.replace(ogImgSrc,newImgSrc);

			}
		}

		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is an image in the MatrixAddin addin, this method will construct the appropriate URI for the
		*  image source.
		*/
		util.handleMatrixAddinImgURIResolving = function(element,cdfkey, matrix, column, rownumber,server) {
			var strHTML = element.innerHTML;

			if(strHTML != '')
			{
			   	var patternToMatch = /src=\"(.)*?\"/;
				var matchResults = patternToMatch.exec(strHTML)[0];
				var imgSrc = matchResults.substring(5,matchResults.length-1);
				
				// IE sometimes appends the http context...only take the image in the end
				var lastSlashIdx = imgSrc.lastIndexOf("/");
				var modImgSrc = imgSrc;
				if(lastSlashIdx != -1)
			        { modImgSrc = modImgSrc.substring(lastSlashIdx+1); }

				modImgSrc = "../../addins/MatrixAdmin/images/matrix/" + modImgSrc;
				var newImgSrc = tibco.admin.Main.resolveURI("tibco.admin",modImgSrc);
				element.innerHTML = strHTML.replace(imgSrc,newImgSrc);
			}
		}

		/* This method will format unformatted status strings. Refer to CR#1-8G7R4C for more information.
		Defect description: Strings representing enumerated types should be mapped to display strings.
		There should be no underbars and only the first letter should be capitalized. For example, RUNNING should be Running.  */
		util.formatStatusStrings = function (element) {
			var formattedString = element.innerHTML;

			if (formattedString == '' || formattedString == null || formattedString == '&nbsp;') {
				return;
			} else {
				formattedString = formattedString.toLowerCase().replace(/_/, " ");
				formattedString = formattedString.replace(formattedString.charAt(0), formattedString.charAt(0).toUpperCase());
				element.innerHTML = formattedString;
			}
		}

		util.formatStatusMessage = function (str) {
			var str = str + "";
			str = str.toLowerCase().replace(/_/, " ");
			str = str.replace(str.charAt(0), str.charAt(0).toUpperCase());
			return str;
		}

		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is an ellapsed time period in nanoseconds, this method will construct
		*  the appropriate displayable string
		*/
		util.handleElapsedTimeFormattingInNanos = function(element,cdfkey, matrix, column, rownumber,server) {
			var timeInNanos = element.innerHTML;
			timeInNanos = timeInNanos.replace("&nbsp;","");
			timeInNanos = timeInNanos.trim();
			var timeInMillis = timeInNanos/1000000;
			var formattedTime = util.formatElapsedTime(timeInMillis);
			element.innerHTML = formattedTime;
		}
		
		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is an ellapsed time period in milliseconds, this method will construct
		*  the appropriate displayable string
		*/
		util.handleElapsedTimeFormattingInMillis = function(element,cdfkey, matrix, column, rownumber,server) {
			var timeInMillis = element.innerHTML;
			timeInMillis = timeInMillis.replace("&nbsp;","");
			timeInMillis = timeInMillis.trim();
			var formattedTime = util.formatElapsedTime(timeInMillis);
			element.innerHTML = formattedTime;
		}

		util.formatElapsedTime = function(timeInMillis) {
			var formattedTime = "";
			if(timeInMillis && timeInMillis != "" && timeInMillis != "undefined")
			{
				// pull the necessary externalized Strings
				var minuteString = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@timeFormatMinute");
				var hourString = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@timeFormatHour");
				var dayString = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@timeFormatDay");
				timeInMillis = util.stripDecimal(timeInMillis);

				if(timeInMillis.indexOf(minuteString) != -1)
				{
					// the time has already been formatted or the value is invalid, return the original value
					return timeInMillis;
				}

				// first convert to minutes
				var mins = (timeInMillis - 0) / (60 * 1000);
				mins = util.stripDecimal(mins);

				if(mins < 60)
				{ formattedTime = "00" + dayString + " 00" + hourString + " " + util.formatToDoubleDigit(mins) + minuteString; }
				else
				{
					mins = mins - 0;
					var hours = mins / 60;
					hours = util.stripDecimal(hours);
					mins = mins % 60;
					if(hours < 24)
					{ formattedTime = "00" + dayString + " " + util.formatToDoubleDigit(hours) + hourString + " " + util.formatToDoubleDigit(mins) + minuteString; }
					else
					{
						var days = hours / 24;
						days = util.stripDecimal(days);
						hours = hours % 24;
						formattedTime = util.formatToDoubleDigit(days) + dayString + " " + util.formatToDoubleDigit(hours) + hourString + " " + util.formatToDoubleDigit(mins) + minuteString;
					}
				}
			}
			return formattedTime;
		}

		util.stripDecimal = function(num) {
			num = num + "";
			var decimalIdx = num.indexOf(".");
			if(decimalIdx != -1)
			{ num = num.substring(0, decimalIdx); }
			return num;
		}

		util.formatToDoubleDigit = function(num) {
			num = num - 0;
			if(num < 10)
			{ return "0" + num; }
			else
			{ return num + ""; }
		}

		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is a non-negative number, this method will return "N/A" if the 
		*  number is negative and if not the passed in value will be returned
		*/
		util.handleNonNegativeFormatting = function(element,cdfkey, matrix, column, rownumber,server) {
			var number = element.innerHTML;
			number = number.replace("&nbsp;","");
			number = number.trim();
			if(number-0 < 0)
			{ number = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@notApplicable"); }
			element.innerHTML = number;
		}
		
		/**
		*  This method is designed for usage in the "Format Handler" property of a jsx3.gui.Matrix.Column.
		*  When the content displayed is an start date in milliseconds, this method will construct
		*  the appropriate displayable string
		*/
		util.handleStartDateFormatting = function(element,cdfkey, matrix, column, rownumber,server) {
			var timeInMillis = element.innerHTML;
			timeInMillis = timeInMillis.replace("&nbsp;","");
			timeInMillis = timeInMillis.trim();
			var formattedTime;
			if(timeInMillis == 0)
			{ formattedTime = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@notApplicable"); }
			else
			{ formattedTime = util.formatStartDateISO(timeInMillis); }
			element.innerHTML = formattedTime;
		}
		
		util.formatStartDateISO = function(timeInMillis)
		{
			timeInMillis = timeInMillis-0;
			if(timeInMillis == 0)
			var formattedDate;
            if(timeInMillis == 0)
            { formattedDate = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@notApplicable"); }
            else
    	    { formattedDate = util.formatDateISO(timeInMillis); }
			return formattedDate;
		}

		util.formatDateISO = function(timeInMillis) 
      {
          var formattedDate = "<unset>";
          try {
                var mf = new jsx3.util.MessageFormat("{0,datetime,short}");
                var specDate = new Date();
                specDate.setTime(timeInMillis);
                formattedDate = mf.format(specDate); 
              }
          catch(badE)
             {
                  badE = jsx3.NativeError.wrap(badE);
                  jsx3.log(badE.toString());
              }
			return formattedDate;
          
      }





	}
);
